<?php


namespace app\common\model;


use think\Db;

class ShopsBanner extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_shops_banner';
    // 主键ID
    protected $pk = 's_b_id';

    //商铺轮播图类型
    public static $paystatelist = [
        '1' => '推广轮播图',
        '2' => '店铺图册',
    ];

    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    function get_list($cond, $orderby, $item,$limit = 10)
    {
        $where = null;
        if(isset($cond['shop_id'])){
            $where[] = ['shop_id','=', $cond['shop_id']];
        }

        if(!empty($cond['type'])){
            $where[] = ['pos', '=', $cond['type']];
        }

        $rs = $this
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->paginate($limit);
        return $rs;
    }

    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ['s_b_id' => $id];
            $rs = $this->allowField(true)->save($params, $where);
        }else{
//            $params['createat'] = time();
            // 更新数据
            $this->allowField(true)->save($params);
            $rs = $this->s_b_id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

    //新增、编辑(后台操作)
    function delshopbanner($bannerid,$shop_id){
        $where[] = ['s_b_id','=',$bannerid];
        if(isset($shop_id)){
            $where[] = ['shop_id','=',$shop_id];
        }
        $rs = $this->where('s_b_id','=',$bannerid)->delete();
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

//    h5获取商品列表
    function get_product_list($cond, $orderby, $item = 'p.*',$limit)
    {
        $where = null;
        if(!empty($cond['product_name'])){
            $where[] = ['p.product_name', 'like', '%'.$cond['product_name'].'%'];
        }
        if(!empty($cond['type_id'])){
            $where[] = ['p.type_id', '=', $cond['type_id']];
        }
        if(!empty($cond['shop_id'])){
            $where[] = ['p.shop_id', '=', $cond['shop_id']];
        }
        if(!empty($cond['approved'])){
            $where[] = ['p.approved', '=', $cond['approved']];
        }
        if(!empty($cond['state'])){
            $where[] = ['p.state', '=', $cond['state']];
        }
        if(!empty($cond['is_del'])){
            $where[] = ['p.is_del', '=', $cond['is_del']];
        }
        if(!empty($cond['shop_approved'])){
            $where[] = ['s.approved', '=', $cond['shop_approved']];
        }
        if(!empty($cond['shop_state'])){
            $where[] = ['s.state', '=', $cond['shop_state']];
        }
        if(!empty($cond['class_id'])){
            $where[] = ['p.class_id', '=', $cond['class_id']];
        }

        $rs = $this
            ->alias('p')
            ->leftJoin('ddt_shops s','p.shop_id = s.shop_id')
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->paginate($limit);
        return $rs;
    }
}